//
//  NSDate+CM_DateTools.m
//  CoolMove
//
//  Created by CA on 15/10/30.
//  Copyright © 2015年 CA. All rights reserved.
//

#import "NSDate+CM_DateTools.h"

@implementation NSDate (CM_DateTools)

- (NSString *)convertMonthDayByWeekDay:(WeekDay)weekDay
{
    NSInteger dayNumber = self.weekday;
    if (dayNumber == 1) {
        dayNumber += 7;
    }
    
    NSInteger differDay = dayNumber - 2;
    
    NSDate *mondayDate = [self dateBySubtractingDays:differDay];
    NSDate *convertDayDate = [mondayDate dateByAddingDays:weekDay - 1];
    
    NSString *convertDayStr = [convertDayDate formattedDateWithFormat:@"MM/dd"];
    
    return convertDayStr;
}


/**
 * @brief 获取两个日期之间的天数
 * @return    总天数
 */
+ (NSInteger)numberOfDaysWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comp = [calendar components:NSCalendarUnitDay
                                         fromDate:fromDate
                                           toDate:toDate
                                          options:NSCalendarWrapComponents];
    return labs(comp.day);
}

/**
 * @brief 获取两个日期之间的相差的月份
 * @return    总月份数
 */
+ (NSInteger)numberOfMonthsWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comp = [calendar components:NSCalendarUnitMonth
                                         fromDate:fromDate
                                           toDate:toDate
                                          options:NSCalendarWrapComponents];
    return labs(comp.month);
}

// 判断两个时间是否在同一个星期
- (BOOL)judgeEqualINWeekDate1:(NSDate *)date1 andDate2:(NSDate *)date2 {
    
    NSString *monday = [date1 convertMonthDayByWeekDay:WeekDayMonday];
    NSString *sunday = [date1 convertMonthDayByWeekDay:WeekDaySunday];
    NSString *startEndDateStr = [NSString stringWithFormat:@"%@-%@", monday, sunday];
    
    NSString *itemMonday = [date2 convertMonthDayByWeekDay:WeekDayMonday];
    NSString *itemSunday = [date2 convertMonthDayByWeekDay:WeekDaySunday];
    NSString *itemStartEndDateStr = [NSString stringWithFormat:@"%@-%@", itemMonday, itemSunday];
    
    if ([itemStartEndDateStr isEqualToString:startEndDateStr]) {
        return YES;
    }else {
        return NO;
    }
}

//将分钟转换成 xx:xx' 格式的  小时制
+ (NSString *)convertToHourMinFormateWithMin:(NSInteger)min{
    if (min < 60) {
        return [NSString stringWithFormat:@"%ldmin",min];
    }else if (min % 60 == 0){
        return [NSString stringWithFormat:@"%ldh",min/60];
    }
    return [NSString stringWithFormat:@"%ldh %ldm",min/60,min%60];
}

/**根据周次获取周次的范围日期*/
+(NSString*)timeConversionYear:(NSInteger)year WeakOfYear:(NSInteger)weekofYear
{
    //周次的范围日期  几月几日 - 几月几日
    NSString *weekDate = @"";
    
    //时间轴 取每一年的六月一号 没有特别的含义
    NSString *timeAxis = [NSString stringWithFormat:@"%ld-06-01 12:00:00",(long)year];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //获得了时间轴
    NSDate *date = [dateFormatter dateFromString:timeAxis];
    
    //日历类 提供大部分的时间计算接口
    NSCalendar *calendar = [NSCalendar currentCalendar];
    /**这两个参数的设置影响着周次的个数和划分*****************/
    [calendar setFirstWeekday:2]; //设置每周的开始是星期一
    [calendar setMinimumDaysInFirstWeek:7]; //设置一周至少需要几天
    /****************/
    //一个封装了具体年月日、时秒分、周、季度等的类
    NSDateComponents *comps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                          fromDate:date];
    
    //时间轴是当前年的第几周
    NSInteger todayIsWeek = [comps weekOfYear];
    //第几周的字符串格式
    //    NSString *todayIsWeekStr = [NSString stringWithFormat:@"%ld",(long)todayIsWeek];
    
    //获取时间轴是星期几 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
    NSInteger todayIsWeekDay = [comps weekday];
    
    //得到时间轴是几号
    //    NSInteger todayIsDay = [comps day];
    
    // 计算当前日期和这周的星期一和星期天差的天数
    //firstDiff 星期一相差天数 、 lastDiff 星期天相差天数
    long firstDiff,lastDiff;
    if (todayIsWeekDay == 1) {
        firstDiff = -6;
        lastDiff = 0;
    }else
    {
        firstDiff = [calendar firstWeekday] - todayIsWeekDay;
        lastDiff = 8 - todayIsWeekDay;
    }
    
    NSDate *firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*firstDiff sinceDate:date];
    NSDate *lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*lastDiff sinceDate:date];
    
    long weekdifference = weekofYear - todayIsWeek;
    
    firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:firstDayOfWeek];
    lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:lastDayOfWeek];
    //    NSLog(@"星期一的日期 %@",[dateFormatter stringFromDate:firstDayOfWeek]);
    //    NSLog(@"星期天的日期 %@",[dateFormatter stringFromDate:lastDayOfWeek]);
    
    NSDateComponents *firstDayOfWeekcomps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                                        fromDate:firstDayOfWeek];
    NSDateComponents *lastDayOfWeekcomps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                                       fromDate:lastDayOfWeek];
    
    NSInteger startMonth = [firstDayOfWeekcomps month];
    NSInteger startDay = [firstDayOfWeekcomps day];
    
    NSInteger endmonth = [lastDayOfWeekcomps month];
    NSInteger endday = [lastDayOfWeekcomps day];
    
    weekDate = [NSString stringWithFormat:@"%ld/%ld-%ld/%ld",(long)startMonth,(long)startDay,(long)endmonth,(long)endday];
    return weekDate;
}
@end
